package menu;

import java.awt.event.KeyEvent;
import java.lang.reflect.Field;

import javax.swing.ImageIcon;

import main.Application;

public class MenuFieldAttributes {
	
	public final static String ACTIONNAME_BROWSER = "browser";
	public final static String ACTIONNAME_JASPER_RAPORT = "jasperRaport"; 
	public final static String ACTIONNAME_CHANGEPASSWORD = "changePassword"; 
	public final static String ACTIONNAME_MANAGEUSERS = "manageUsers"; 
	public final static String ACTIONNAME_ZBOONEXPERIMENT1 = "swingExperiment"; 
	public final static String ACTIONNAME_OPROGRAMIE = "oProgramie"; 
	public final static String ACTIONNAME_KONTAKT = "kontakt"; 
	public final static String ACTIONNAME_CONFIG = "konfiguracja";

	public final static String ATTRIBUTE_LABEL = "label"; //wyswietlany label
	public final static String ATTRIBUTE_LISTENER_NAME = "listener"; //nazwa klasy dziedziczacej po lapaczu wyjatkow, ktora posaida metode menuOperation()
	public final static String ATTRIBUTE_PICTURE = "picture";	 // pojawiajaca sieobok ikonka
	public final static String ATTRIBUTE_TABLENAME = "tableName"; //nazwa tabeli, ktorej dotyczy wolany browser
	public final static String ATTRIBUTE_TABLEWIDTH = "tableWidth"; //szerokosc wolanego browsera
	public final static String ATTRIBUTE_TABLEHEIGHT = "tableHeight"; //wysokosc wolanego browsera
	public final static String ATTRIBUTE_ACTIONNAME = "actionName";  // nazwa klasy z ktorej ma byc odpalana akcja (bez refleksji)
	public final static String ATTRIBUTE_MNEMONIC = "hotKey"; //ustawia skrot klawiszowy
	public final static String ATTRIBUTE_ADMIN_ONLY = "adminOnly"; //pozycja bedzie widoczna tylko jesli zalogowany bedzie admin
	public final static String ATTRIBUTE_JASPER_RAPORT_PARAMS_NAME = "raportParamsName"; //nazwa zestawy parametrow, czyli atrybut name tagu params
	public final static String ATTRIBUTE_JASPER_RAPORT_PARAMS_SRC = "raportParamsSrc"; // sciezka do pliku, w ktorym beda szukane parametry	
	
	public final static String ACTIONNAME_UPLOADFILE = "uploadFile";
	//TODO atrybuty do odpalania edytek (odpowiedniki tych dla browserow)
	
	private String label;
	private String listenerName;
	private ImageIcon picture;
	private String tableName;
	private String actionName;
	private Integer tableWidth;
	private Integer tableHeight;
	private int mnemonic;
	private boolean adminOnly;
	private String raportParamsName;
	private String raportParamsSrc;
	
	public MenuFieldAttributes(String label, String listenerName, String actionName,
			String tableName, String tableWidth, String tableHeight, String picture, String mnemo, String raportParamsName, String raportParamsSrc) throws Exception
	{
		this.label=label;
		this.listenerName = listenerName;
		this.raportParamsName=raportParamsName;
		this.raportParamsSrc = raportParamsSrc;
		this.tableName = tableName;
		this.actionName = actionName;
		if (tableHeight.equals("")) 
			this.tableHeight = 0;
		else
			this.tableHeight = Integer.parseInt(tableHeight);

		if (tableWidth.equals("")) 
			this.tableWidth = 0;
		else
			this.tableWidth = Integer.parseInt(tableWidth);
        this.picture = createImageIcon(picture);
        this.mnemonic = createMnemonic (mnemo);
	}
	public String getLabel()
	{
		return label;
	}
	public String getListenerName()
	{
		return listenerName;
	}
	public String getTableName()
	{
		return tableName;
	}
	public Integer getTableWidth()
	{
		return tableWidth;		
	}
	public Integer getTableHeight()
	{
		return tableHeight;		
	}
	public String getActionName() {
		return actionName;
	}
	public void setLabel(String label)
	{
		 this.label=label;
	}
	public void setListenerName(String listenerName)
	{
		this.listenerName=listenerName;
	}
	public void setTableName(String tableName)
	{
		this.tableName=tableName;
	}
	public void setTableWidth(Integer tableWidth)
	{
		this.tableWidth=tableWidth;		
	}
	public void setTableHeight(Integer tableHeight)
	{
		this.tableHeight=tableHeight;		
	}
	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

    /** Returns an ImageIcon, or null if the path was invalid. */
    private ImageIcon createImageIcon(String path) {
        if (path != null && path!="") {
            return new ImageIcon(path);
        } else {
            if (path == "") return null;
        	System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

    public ImageIcon getPicture() {
		return picture;
	}
	public void setPicture(ImageIcon picture) {
		this.picture = picture;
	}
	public int getMnemonic() {
		return mnemonic;
	}
	public void setMnemonic(int mnemonic) {
		this.mnemonic = mnemonic;
	}
	
	private int createMnemonic (String mnemo)
	{
		if (mnemo == "")
			return -1;
		mnemo = mnemo.toUpperCase();
		int key=-1;
		Class cls = KeyEvent.class;
		try
		{
			Field fl = cls.getDeclaredField("VK_" + mnemo);
			key = fl.getInt(null);
		}
		catch (Exception e)
		{
			Application.getInstance().notifyAboutException(e);
		}
		return key;
	}
	public boolean isAdminOnly() {
		return adminOnly;
	}
	public void setAdminOnly(boolean adminOnly) {
		this.adminOnly = adminOnly;
	}
	public String getRaportParamsName() {
		return raportParamsName;
	}
	public void setRaportParamsName(String raportParamsName) {
		this.raportParamsName = raportParamsName;
	}
	public String getRaportParamsSrc() {
		return raportParamsSrc;
	}
	public void setRaportParamsSrc(String raportParamsSrc) {
		this.raportParamsSrc = raportParamsSrc;
	}
	
}
